/*     */ package com.newcrm.db;
/*     */ 
/*     */ import com.newcrm.bean.User;
/*     */ import com.newcrm.exception.DBException;
/*     */ import java.sql.Connection;
/*     */ import java.sql.SQLException;
/*     */ import java.util.Calendar;
/*     */ import java.util.Date;
/*     */ import java.util.List;
/*     */ import java.util.Map;
/*     */ import org.apache.commons.dbutils.DbUtils;
/*     */ import org.apache.commons.dbutils.GenKeyQueryRunner;
/*     */ import org.apache.commons.dbutils.MyMapListHandler;
/*     */ import org.apache.commons.dbutils.QueryRunner;
/*     */ import org.apache.commons.dbutils.handlers.MapListHandler;
/*     */ import org.apache.commons.dbutils.handlers.ScalarHandler;
/*     */ import org.apache.log4j.Logger;
/*     */ 
/*     */ public class ImportantDateManager
/*     */ {
/*  24 */   private static Logger logger = Logger.getLogger(ImportantDateManager.class
/*  25 */     .getName());
/*     */ 
/*     */   public int getImportantDateNum(String userId)
/*     */   {
/*  35 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  36 */     if (conn == null) {
/*  37 */       return 0;
/*     */     }
/*     */ 
/*  40 */     Calendar cal = Calendar.getInstance();
/*  41 */     int year = cal.get(1);
/*  42 */     int month = cal.get(2) + 1;
/*  43 */     int day = cal.get(5);
/*     */ 
/*  45 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/*  47 */       Object obj = runner.query(conn, "SELECT COUNT(*) FROM T_ImportantDate_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId WHERE B.DeleteFlag = 0 AND B.Visible = ? AND B.PermissionsId = ? AND A.ImportantDateMonth = ? AND A.ImportantDateDay = ? AND (A.ImportantDateYear = 0 OR A.ImportantDateYear = ?) ", 
/*  48 */         new ScalarHandler(1), new Object[] { Integer.valueOf(4), userId, Integer.valueOf(month), 
/*  49 */         Integer.valueOf(day), Integer.valueOf(year) });
/*  50 */       if (!(obj instanceof Long)) {
/*  51 */         return 0;
/*     */       }
/*  53 */       return ((Long)obj).intValue();
/*     */     } catch (SQLException e) {
/*  55 */       logger.warn("get count failed", e);
/*     */     } finally {
/*     */       try {
/*  58 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/*  60 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*  63 */     return 0;
/*     */   }
/*     */ 
/*     */   public List<Map<String, Object>> getList(Date date, String userId, int offset, int pageSize)
/*     */     throws DBException, SQLException
/*     */   {
/*  80 */     Calendar cal = Calendar.getInstance();
/*     */ 
/*  82 */     if (date != null) {
/*  83 */       cal.setTime(date);
/*     */     }
/*  85 */     int year = cal.get(1);
/*  86 */     int month = cal.get(2) + 1;
/*  87 */     int day = cal.get(5);
/*     */ 
/*  89 */     Connection conn = ConnectionManager.getInstance().getConnection();
/*  90 */     if (conn == null) {
/*  91 */       throw new DBException();
/*     */     }
/*  93 */     QueryRunner runner = new QueryRunner();
/*  94 */     StringBuffer sql = new StringBuffer();
/*  95 */     sql.append("SELECT A.ImportantDateType, A.CustomerId, A.ImportantDateMonth, A.ImportantDateDay, A.ImportantDateYear, B.Type, B.Name FROM T_ImportantDate_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId WHERE B.DeleteFlag = 0 AND B.Visible = ? AND B.PermissionsId = ? AND A.ImportantDateMonth = ? AND A.ImportantDateDay = ? AND (A.ImportantDateYear = 0 OR A.ImportantDateYear = ?) ORDER BY A.CreateDate DESC ").append(" LIMIT ")
/*  96 */       .append(offset).append(", ").append(pageSize);
/*     */     try {
/*  98 */       return (List)runner.query(conn, "SELECT A.ImportantDateType, A.CustomerId, A.ImportantDateMonth, A.ImportantDateDay, A.ImportantDateYear, B.Type, B.Name FROM T_ImportantDate_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId WHERE B.DeleteFlag = 0 AND B.Visible = ? AND B.PermissionsId = ? AND A.ImportantDateMonth = ? AND A.ImportantDateDay = ? AND (A.ImportantDateYear = 0 OR A.ImportantDateYear = ?) ORDER BY A.CreateDate DESC ", 
/*  99 */         new MapListHandler(), new Object[] { Integer.valueOf(4), userId, Integer.valueOf(month), 
/* 100 */         Integer.valueOf(day), Integer.valueOf(year) });
/*     */     } finally {
/*     */       try {
/* 103 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 105 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public List<Map<String, Object>> getLastImportDates(int year, int month, int day)
/*     */     throws DBException, SQLException
/*     */   {
/* 124 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 125 */     if (conn == null) {
/* 126 */       throw new DBException();
/*     */     }
/* 128 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 130 */       return (List)runner.query(conn, "SELECT A.LastGenerateDate, A.ImportantDateId,A.ImportantDateType,B.Name,B.PermissionsId,B.Type,B.CompanyId,A.CustomerId FROM T_ImportantDate_Info AS A INNER JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId WHERE B.DeleteFlag = 0 AND A.ImportantDateMonth = ? AND A.ImportantDateDay = ? AND (A.ImportantDateYear = 0 OR A.ImportantDateYear = ?) ", 
/* 131 */         new MapListHandler(), new Object[] { Integer.valueOf(month), Integer.valueOf(day), Integer.valueOf(year) });
/*     */     } finally {
/*     */       try {
/* 134 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 136 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/*     */   }
/*     */ 
/*     */   public List<Map<String, Object>> list(int customerId)
/*     */   {
/* 149 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 150 */     if (conn == null) {
/* 151 */       logger.warn("conn is null");
/* 152 */       return null;
/*     */     }
/* 154 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 156 */       return (List)runner.query(conn, "SELECT ImportantDateId,ImportantDateType,ImportantDateMonth,ImportantDateDay,ImportantDateYear FROM T_ImportantDate_Info WHERE CustomerId = ? ORDER BY CreateDate DESC", 
/* 157 */         new MyMapListHandler(), new Object[] { Integer.valueOf(customerId) });
/*     */     } catch (SQLException e) {
/* 159 */       logger.warn("list important date failed", e);
/*     */     } finally {
/*     */       try {
/* 162 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 164 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 167 */     return null;
/*     */   }
/*     */ 
/*     */   public int add(int customerId, String type, int month, int day, int year, int companyId, String userId)
/*     */   {
/* 184 */     if (companyId < 0) {
/* 185 */       return -1;
/*     */     }
/*     */ 
/* 188 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 189 */     if (conn == null) {
/* 190 */       return -1;
/*     */     }
/*     */ 
/* 193 */     GenKeyQueryRunner runner = new GenKeyQueryRunner(
/* 194 */       new ScalarHandler(), new String[] { "ImportantDateId" });
/*     */     try {
/* 196 */       runner.update(conn, "INSERT INTO T_ImportantDate_Info (CustomerId, ImportantDateType, ImportantDateMonth, ImportantDateDay, ImportantDateYear, CompanyId, CreateUserId, CreateDate,UpdateDate) values (?, ?, ?, ?, ?, ?, ?, NOW(), NOW()) ", new Object[] { Integer.valueOf(customerId), type, 
/* 197 */         Integer.valueOf(month), Integer.valueOf(day), Integer.valueOf(year), Integer.valueOf(companyId), userId });
/* 198 */       return ((Long)runner.getGeneratedKeys()).intValue();
/*     */     } catch (SQLException e) {
/* 200 */       logger.warn("create important date failed", e);
/*     */     } finally {
/*     */       try {
/* 203 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 205 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 208 */     return -1;
/*     */   }
/*     */ 
/*     */   public int delete(int importantDateId, User loginUser)
/*     */   {
/* 218 */     if (importantDateId < 0) {
/* 219 */       return 0;
/*     */     }
/*     */ 
/* 222 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 223 */     if (conn == null) {
/* 224 */       return -1;
/*     */     }
/*     */ 
/* 227 */     QueryRunner runner = new QueryRunner();
/*     */     try
/*     */     {
/*     */       int i;
/* 230 */       if (loginUser.isAdmin) {
/* 231 */         return runner.update(conn, 
/* 232 */           "DELETE FROM T_ImportantDate_Info WHERE ImportantDateId = ? ", Integer.valueOf(importantDateId));
/*     */       }
/* 234 */       return runner.update(conn, "DELETE A FROM T_ImportantDate_Info AS A LEFT JOIN T_Customer_Info AS B ON A.CustomerId = B.CustomerId WHERE A.ImportantDateId = ? AND B.Visible = ? AND B.PermissionsId = ? ", new Object[] { 
/* 235 */         Integer.valueOf(importantDateId), Integer.valueOf(4), loginUser.userId });
/*     */     } catch (SQLException e) {
/* 237 */       logger.warn("delete important date failed", e);
/*     */     } finally {
/*     */       try {
/* 240 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 242 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 245 */     return 0;
/*     */   }
/*     */ 
/*     */   public int update(int importantDateId, String type, int month, int day, int year, String userId)
/*     */   {
/* 261 */     if (importantDateId < 0) {
/* 262 */       return 0;
/*     */     }
/*     */ 
/* 265 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 266 */     if (conn == null) {
/* 267 */       return -1;
/*     */     }
/* 269 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 271 */       return runner.update(conn, "UPDATE T_ImportantDate_Info SET ImportantDateType = ?, ImportantDateMonth = ?, ImportantDateDay = ?, ImportantDateYear = ?, UpdateDate = NOW(), UpdateUserId = ?, LastGenerateDate = null WHERE ImportantDateId = ?", new Object[] { type, 
/* 272 */         Integer.valueOf(month), Integer.valueOf(day), Integer.valueOf(year), userId, Integer.valueOf(importantDateId) });
/*     */     } catch (SQLException e) {
/* 274 */       logger.warn("update important date failed", e);
/*     */     } finally {
/*     */       try {
/* 277 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 279 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 282 */     return 0;
/*     */   }
/*     */ 
/*     */   public int updateLastGenerateDate(int importantDateId)
/*     */   {
/* 292 */     if (importantDateId < 0) {
/* 293 */       return 0;
/*     */     }
/*     */ 
/* 296 */     Connection conn = ConnectionManager.getInstance().getConnection();
/* 297 */     if (conn == null) {
/* 298 */       return -1;
/*     */     }
/* 300 */     QueryRunner runner = new QueryRunner();
/*     */     try {
/* 302 */       return runner.update(conn, 
/* 303 */         "UPDATE T_ImportantDate_Info SET LastGenerateDate = NOW() WHERE ImportantDateId = ?", 
/* 304 */         Integer.valueOf(importantDateId));
/*     */     } catch (SQLException e) {
/* 306 */       logger.warn("update LastGenerateDate failed", e);
/*     */     } finally {
/*     */       try {
/* 309 */         DbUtils.close(conn);
/*     */       } catch (SQLException e) {
/* 311 */         logger.warn("close conn failed", e);
/*     */       }
/*     */     }
/* 314 */     return 0;
/*     */   }
/*     */ }

/* Location:           C:\E\referenceProject\CRM参考\qikebao\WEB-INF\lib\qikebao.jar
 * Qualified Name:     com.newcrm.db.ImportantDateManager
 * JD-Core Version:    0.6.1
 */